package com.sicheng.algorithm.array;

import org.junit.Test;

import java.util.Arrays;

public class BinarySearch {

    @Test
    public void test(){
        int[] arr ={1,0,9,5,7,2,3,4};
        Arrays.sort(arr);
        System.out.println(binarySearch(arr, 99));
    }
    public int binarySearch(int[] nums, int key) {
        int low = 0, high = nums.length - 1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (nums[mid] == key) {
                return mid;
            } else if (nums[mid] > key) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }
}
